<template>
  <h1>表单组件</h1>
  <el-form style="width: 500px;margin: 0 auto;" label-width="80px">
    <el-form-item label="用户名">
      <el-input v-model="user.username" placeholder="请输入用户名"></el-input>
    </el-form-item>
    <el-form-item label="密码">
      <el-input v-model="user.password" placeholder="请输入密码" show-password></el-input>
    </el-form-item>
    <el-form-item label="性别">
      <el-radio-group v-model="user.gender">
        <el-radio label="男" value="1"></el-radio>
        <el-radio label="女" value="0"></el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="所在地">
      <el-select v-model="user.city" placeholder="请选择">
        <el-option label="上海" value="sh"></el-option>
        <el-option label="北京" value="bj"></el-option>
        <el-option label="广州" value="gz"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="管理员">
      <el-switch v-model="user.isAdmin"></el-switch>
    </el-form-item>
    <el-form-item>
      <el-button type="success" @click="reg">点我提交</el-button>
    </el-form-item>
  </el-form>
  <hr>
  <p v-if="isShow">我是一个会隐身的段落1</p>
  <p v-show="isShow">我是一个会隐身的段落2</p>
  <button @click="change">点我隐身</button>
  <hr>
  <input type="text" v-model="imgUrl" placeholder="请输入图片URL">
  <img :src="imgUrl">
  <hr>
  <ul>
    <li v-for="(item,index) in goods">
      {{item}}
      <el-button @click="del(index)" type="danger">删除</el-button>
    </li>
  </ul>
  <hr>
  <el-form style="width: 600px;margin: 0 auto;" label-width="80px">
    <el-form-item label="用户名">
      <el-input v-model="user2.username" placeholder="请输入用户名" clearable></el-input>
    </el-form-item>
    <el-form-item label="密码">
      <el-input v-model="user2.password" placeholder="请输入密码" show-password></el-input>
    </el-form-item>
    <el-form-item label="确认密码">
      <el-input v-model="user2.confirmPassword" placeholder="请输入确认密码" show-password></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="reg2">点我提交</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import {ref} from "vue";
//1.创建一个按钮点击按钮可以切换p标签的显示状态
//要求使用 v-if 控制第一个段落是否渲染,用 v-show 控制第二个段落的可见性
const isShow = ref(true);
const change = () => {
  isShow.value = !isShow.value;
}
//2. 创建一个动态图片展示组件,包含一个输入框,当用户输入图片URL后
//下方的 <img> 元素将动态绑定并显示该图片
const imgUrl = ref('');
//3.定义一个包含至少5个商品名称的数组,利用 v-for 将其渲染为无序列表,并为每个商品添加删除功能
const goods = ref(["西瓜", "香蕉", "苹果", "橘子", "哈密瓜"]);
const del = (index) => {
  goods.value.splice(index,1);
}
//4. 设计一个表单,包括用户名、密码、确认密码字段
//当用户点击提交按钮时,验证两次输入的密码是否一致,并输出提示信息
const user2 = ref({
  username:'',
  password:'',
  confirmPassword:''
});
//定义注册的方法
const reg2 = ()=>{
  if(user2.value.password !== user2.value.confirmPassword){
    alert("两次输入的密码不一致!");
  }else{
    alert("注册成功!");
  }
}


//定义对象用来保存从表单中收集到的数据
const user = ref({
  username:'',
  password:'',
  gender:'',
  city:'',
  isAdmin:false
});
//定义注册的方法
const reg = () => {
  console.log(user.value);
  console.log(user.value.username);
}
</script>

<style scoped>

</style>